package ztc.tools;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.*;
import java.util.List;

/**
 * Created by ztc on 16-5-31.
 */
public class Net {
    private static CloseableHttpClient client;
    static {
        client= HttpClients.createDefault();
    }
    public static String doGet(String url) {
        HttpGet get = new HttpGet(url);
        get.addHeader("Content-Type","text/html;charset=UTF-8");
        RequestConfig requestConfig=RequestConfig.custom()
                .setSocketTimeout(10000)
                .setConnectTimeout(10000).build();
        //get.setConfig(requestConfig);
        StringBuilder str = new StringBuilder();
        try {
            CloseableHttpResponse response = client.execute(get);
            HttpEntity he = response.getEntity();
            String respContent = EntityUtils.toString(he, "UTF-8").trim();
            return respContent;
            /*
            InputStream is = he.getContent();
            byte[] buffer = new byte[1024];
            int count;
            while ((count = is.read(buffer)) != -1) {
                String bu = new String(buffer, 0, count);
                str.append(bu);
            }
            is.close();
            */
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str.toString();
    }

    public static void saveInfo(List list, String path,String xflink) {
        int i=0;
        String content=null;
        File file=new File(path);
        if(!file.isDirectory())
            file.mkdir();
        for(Object o:list) {
            content+=o.toString();
            content+="\n";
        }
        FileOutputStream fileOutputStream=null;
        try {
            fileOutputStream=new FileOutputStream(path+"/xfplay.txt");
            fileOutputStream.write((content+xflink).getBytes());
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        /*
        for(Object o:list) {
            i++;
            HttpGet get = new HttpGet(o.toString());
            try {
                FileOutputStream fos=new FileOutputStream(path+"/"+i+".jpg");
                CloseableHttpResponse response = client.execute(get);
                HttpEntity he = response.getEntity();
                InputStream is = he.getContent();
                byte[] buffer = new byte[1024];
                int count;
                while ((count = is.read(buffer)) != -1) {
                    fos.write(buffer,0,count);
                }
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        */
    }
}
